package android.com.br.braziltvguide;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import android.com.br.braziltvguide.Lista;
import java.text.*;
import java.util.*;

public class LerArquivosXML extends DefaultHandler {
	
	Boolean blnPt = false;
	Boolean blnCategoria = true;
	StringBuffer currentValue = new StringBuffer(50);
	String CanalFull = "";
	
	public static Lista sitesList = null;
	
	public static Lista getLista() {
		return sitesList;
	}

	public static void setSitesList(Lista sitesList) {
		LerArquivosXML.sitesList = sitesList;
	}
	
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {

		if (localName.equalsIgnoreCase("tv"))
		{
			sitesList = new Lista();}
		else if (localName.equals("programme")) {

			for (int i=0; i< attributes.getLength(); i++){
	
				if (attributes.getLocalName(i).equalsIgnoreCase("channel")){
					sitesList.setProgramacao(attributes.getValue(i));
				} else if (attributes.getLocalName(i).equalsIgnoreCase("start")){
					
					Date date;
					DateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");  
					SimpleDateFormat ffinal = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
					
					try {
						date = (java.util.Date)formatter.parse(attributes.getValue(i).substring(0, 14));
						sitesList.setInicio(ffinal.format(date));
					} catch (ParseException e) {
						e.printStackTrace();
					}
					
				} else if (attributes.getLocalName(i).equalsIgnoreCase("stop")){

					Date date;
					DateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");  
					SimpleDateFormat ffinal = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");

					try {
						date = (java.util.Date)formatter.parse(attributes.getValue(i).substring(0, 14));
						sitesList.setFim(ffinal.format(date));
					} catch (ParseException e) {
						e.printStackTrace();
					}
				}
			}
		}
		else if (localName.equals("title")){
			for (int i=0; i< attributes.getLength(); i++){
				if (attributes.getLocalName(i).equalsIgnoreCase("lang")){
					if(attributes.getValue(i).equalsIgnoreCase("pt"))
						blnPt = true;
					else
						blnPt = false;
				}
			}
		}
		else if(localName.equalsIgnoreCase("icon")){
			for (int i=0; i< attributes.getLength(); i++){
				if (attributes.getLocalName(i).equalsIgnoreCase("src")){
					sitesList.setIcone(attributes.getValue(i));}
			}
		}
		else if(localName.equalsIgnoreCase("channel")){
			for (int i=0; i< attributes.getLength(); i++){
				if (attributes.getLocalName(i).equalsIgnoreCase("id")){
					sitesList.setCanal(attributes.getValue(i));}
			}
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {

		if (localName.equalsIgnoreCase("title")){
			if(blnPt)
				sitesList.setTitulopt(currentValue.toString().trim());
		} else if (localName.equalsIgnoreCase("category")){
			if(blnCategoria){
				sitesList.setCategoria(currentValue.toString().trim());
				blnCategoria = false;}
			else
				blnCategoria = true;
		} else if (localName.equalsIgnoreCase("value")){
			sitesList.setRestricao(currentValue.toString().trim());
		} 
		
		else if (localName.equalsIgnoreCase("display-name")){
			if (CanalFull=="")
				sitesList.setCanalfull(currentValue.toString().trim());
			else
				sitesList.setCanalfull(CanalFull);
			
			CanalFull = currentValue.toString().trim();
		} 
		
		currentValue.delete(0, currentValue.length());
		
	}
	
	
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		currentValue.append(ch,start,length);
	}
}
